﻿Imports System.Text
Imports QBuilder

<TestClass()>
Public Class SimpleQueryTest

    <TestMethod()>
    Public Sub SimpleSelect_Constructor1()
        Dim target As SelectQuery = New SelectQuery("SomeTable")
        target.Fields.Add("A")

        Dim expected As String = "SELECT A FROM SomeTable"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub SimpleSelect_ToString()
        Dim target As SelectQuery = New SelectQuery("SomeTable")
        target.Fields.Add("A")

        Dim expected As String = "QBuilder Query: SELECT A FROM SomeTable"
        Dim actual As String
        actual = target.ToString
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub SimpleSelect_1Field()
        Dim target As SelectQuery = New SelectQuery("SomeTable")
        target.Fields.Add("A")

        Dim expected As String = "SELECT A FROM SomeTable"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub SimpleSelect_MultipleFields1()
        Dim target As SelectQuery = New SelectQuery()
        target.Fields.Add("A")
        target.Fields.Add("B")
        target.Fields.Add("C")
        target.TableName = "SomeTable"

        Dim expected As String = "SELECT A, B, C FROM SomeTable"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub SimpleSelect_MultipleFields2()
        Dim target As SelectQuery = New SelectQuery()
        target.AddFields(New String() {"A", "B", "C"})
        target.TableName = "SomeTable"

        Dim expected As String = "SELECT A, B, C FROM SomeTable"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub SimpleSelect_Where1()
        Dim target As SelectQuery = New SelectQuery()
        target.Fields.Add("A")
        target.TableName = "SomeTable"

        target.AddCriteria(New Criteria("Field1", Operation.EqualTo, "1"))

        Dim expected As String = "SELECT A FROM SomeTable WHERE Field1 = 1"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub SimpleSelect_Where2()
        Dim target As SelectQuery = New SelectQuery()
        target.Fields.Add("A")
        target.TableName = "SomeTable"

        target.AddCriteria(New Criteria("Field1", Operation.EqualTo, "1"))
        target.AddCriteria(New Criteria("Field2", Operation.EqualTo, "2"))

        Dim expected As String = "SELECT A FROM SomeTable WHERE Field1 = 1 AND Field2 = 2"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub

    ''' <summary>
    ''' Tests the Simplified method of adding criteria.
    ''' </summary>
    ''' <remarks></remarks>
    <TestMethod()>
    Public Sub SimpleSelect_Where3()
        Dim target As SelectQuery = New SelectQuery()
        target.Fields.Add("A")
        target.TableName = "SomeTable"

        target.AddCriteria("Field1", Operation.EqualTo, "1")

        Dim expected As String = "SELECT A FROM SomeTable WHERE Field1 = 1"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub

    ''' <summary>
    ''' Tests the Simplified method of adding criteria.
    ''' </summary>
    ''' <remarks></remarks>
    <TestMethod()>
    Public Sub SimpleSelect_Where4()
        Dim target As SelectQuery = New SelectQuery()
        target.Fields.Add("A")
        target.TableName = "SomeTable"

        target.AddCriteria("Field1", Operation.EqualTo, "1")
        target.AddCriteria("Field2", Operation.EqualTo, "2", BooleanOperator.Or)

        Dim expected As String = "SELECT A FROM SomeTable WHERE Field1 = 1 OR Field2 = 2"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub SimpleSelect_OrderBy()
        Dim target As SelectQuery = New SelectQuery("SomeTable")
        target.Fields.Add("A")
        target.OrderBy = "A DESC"

        Dim expected As String = "SELECT A FROM SomeTable ORDER BY A DESC"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub SimpleSelect_NoCriteria_NestedGroup()
        Dim target As SelectQuery = New SelectQuery("SomeTable")
        target.Fields.Add("A")

        Dim g As New CriteriaGroup
        target.AddCriteria(g)

        Dim expected As String = "SELECT A FROM SomeTable"
        Dim actual As String
        actual = target.GetSQLText
        Assert.AreEqual(expected, actual)
    End Sub

    ''' <summary>
    ''' Check that setting renderParens is ignored
    ''' </summary>
    ''' <remarks></remarks>
    <TestMethod()>
    Public Sub SimpleSelect_RenderParens()
        Dim target As SelectQuery = New SelectQuery()
        target.RenderParens = True
        Assert.IsFalse(target.RenderParens)
        target.RenderParens = False
        Assert.IsFalse(target.RenderParens)
    End Sub

    <TestMethod()>
    Public Sub SimpleSelect_TableName()
        Dim target As New SelectQuery()
        target.TableName = "Test"
        Assert.AreEqual("Test", target.TableName)
    End Sub
End Class